package com.wiscamp.ninechapters.panels.domain.entities;

import com.wiscamp.ninechapters.panels.domain.enums.RoleTypes;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import cube.ddd.domain.Entity;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

@Data
public class Role implements Entity {
    /**
     * 角色编号
     */
    private long roleId = 0L;

    /**
     * 应用编号
     */
    private long applicationId = 0L;

    /**
     * 角色代码
     */
    private String roleCode = "";

    /**
     * 角色名称
     */
    private String roleName = "";

    /**
     * 角色类型
     */
    private long roleTypeId = 0L;

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 创建用户
     */
    private long creatorId = 0L;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0L;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 角色类型名称
     */
    private String roleTypeName = "";

    /**
     * 创建人名称
     */
    private String creatorName = "";

    /**
     * 编辑人名称
     */
    private String modifierName = "";

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    //region roleMenus
    /**
     * 用户列表
     */
    private Collection<Long> menuIds = new ArrayList<>();
    //endregion

    //region rolePermissions
    /**
     * 角色列表
     */
    private Collection<Long> permissionIds = new ArrayList<>();
    //endregion

    public Role(long appId, String roleCode, String roleName, RoleTypes roleType) {
        this.setApplicationId(appId);
        this.setRoleCode(roleCode);
        this.setRoleName(roleName);
        if (Objects.nonNull(roleType)) {
            this.setRoleTypeId(roleType.getIndex());
        }
    }
}
